﻿using System;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.UI.MobileControls;
using Microsoft.JCI.Common.Utility;
using System.Collections.Generic;
using Microsoft.JCI.Bll;

namespace Microsoft.JCI.Portal.Query.Agent
{
    public partial class VendorEntityQuery : BasePage 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropdownList();
            }
        }

        private List<SqlDbParameter> GetQueryCondition()
        {
            List<SqlDbParameter> listParameters = new List<SqlDbParameter>();
            SqlDbParameter Parameter = null;

            if (!string.IsNullOrEmpty(this.txtName.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Name";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Name";
                Parameter.ParameterValue = this.txtName.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtDesc.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Description";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Description";
                Parameter.ParameterValue = this.txtDesc.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtPerson.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "ConnectPersonName1";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@ConnectPersonName1";
                Parameter.ParameterValue = this.txtPerson.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtPhone.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "ConnectPersonPhone1";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@ConnectPersonPhone1";
                Parameter.ParameterValue = this.txtPhone.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.txtAddress.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "Address";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@Address";
                Parameter.ParameterValue = this.txtAddress.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (!string.IsNullOrEmpty(this.ddBranch.SelectedValue))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "ParentCompanyCode";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@ParentCompanyCode";
                Parameter.ParameterValue = ddBranch.SelectedValue;
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                listParameters.Add(Parameter);
            }

            Parameter = new SqlDbParameter();
            Parameter.ColumnName = "Type";
            Parameter.ColumnType = DbType.Int32;
            Parameter.ParameterName = "@Type";
            Parameter.ParameterValue = (int)Enumerator.CompanyType.Vendor;
            Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            listParameters.Add(Parameter);

            return listParameters;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SystemBll bll = new SystemBll();
            DataTable dt = bll.QueryVendorEntity(GetQueryCondition());
            this.radGridCustomerList.DataSource = dt;
            radGridCustomerList.DataBind();
        }

        protected void radGridCustomerList_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            SystemBll bll = new SystemBll();
            DataTable dt = bll.QueryVendorEntity(GetQueryCondition());
            this.radGridCustomerList.DataSource = dt;
        }

        private void BindDropdownList()
        {
            SystemBll bll = new SystemBll();
            
            List<SqlDbParameter> listParameters = new List<SqlDbParameter>();
            SqlDbParameter Parameter = null;

            Parameter = new SqlDbParameter();
            Parameter.ColumnName = "Type";
            Parameter.ColumnType = DbType.String;
            Parameter.ParameterName = "@Type";
            Parameter.ParameterValue = (int)Enumerator.CompanyType.Branch;
            Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            listParameters.Add(Parameter);
            if (CurrentUserEntity.CompanyCode != null)
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "CompanyCode";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@CompanyCode";
                Parameter.ParameterValue = CurrentUserEntity.QueryFilterCompanyCode();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.In;
                listParameters.Add(Parameter);
            }
            DataTable dt = bll.QueryCompanyEntity(listParameters);

            this.ddBranch.DataSource = dt;
            this.ddBranch.DataTextField = "Name";
            this.ddBranch.DataValueField = "CompanyCode";
            ddBranch.Items.Add(new ListItem("...",string.Empty));
            this.ddBranch.DataBind();
        }
    }
}
